<template>
    <el-card class="!border-none" shadow="never">
        <el-alert
            title="设置库存、订单自动取消、完成时间等信息"
            :closable="false"
            show-icon
        ></el-alert>
    </el-card>
    <el-card class="!border-none mt-[15px]" shadow="never">
        <div class="font-medium mb-7">订单设置</div>
        <el-form ref="formRef" :model="formData" label-width="150px">
            <el-form-item label="系统自动取消订单">
                <div>
                    <el-radio-group v-model="formData.cancel_unpaid_orders">
                        <el-radio :label="1">开启</el-radio>
                        <el-radio :label="0">关闭</el-radio>
                    </el-radio-group>
                    <div class="w-[180px]">
                        <el-input
                            v-model="formData.cancel_unpaid_orders_times"
                            placeholder="请输入"
                        >
                            <template #append>分钟</template>
                        </el-input>
                    </div>

                    <div class="form-tips">未付款订单多久时间后自动关闭</div>
                </div>
            </el-form-item>
            <el-form-item label="订单允许取消时长">
                <div>
                    <el-radio-group v-model="formData.cancel_paid_orders">
                        <el-radio :label="1">开启</el-radio>
                        <el-radio :label="0">关闭</el-radio>
                    </el-radio-group>
                    <div class="w-[180px]">
                        <el-input v-model="formData.cancel_paid_orders_times" placeholder="请输入">
                            <template #append>分钟</template>
                        </el-input>
                    </div>

                    <div class="form-tips">已支付但未发货的订单多久时间内允许用户自行取消</div>
                </div>
            </el-form-item>
            <el-form-item label="买家售后维权时效">
                <div>
                    <el-radio-group v-model="formData.after_sales">
                        <el-radio :label="1">开启</el-radio>
                        <el-radio :label="0">关闭</el-radio>
                    </el-radio-group>
                    <div class="w-[180px]">
                        <el-input v-model="formData.after_sales_times" placeholder="请输入">
                            <template #append>天</template>
                        </el-input>
                    </div>

                    <div class="form-tips">订单确认收货多久时间内可以后可申请售后维权</div>
                </div>
            </el-form-item>
        </el-form>
    </el-card>
    <el-card class="!border-none mt-[15px]" shadow="never">
        <div class="font-medium mb-7">库存设置</div>

        <el-form ref="formRef" :model="formData" label-width="150px">
            <el-form-item label="库存占用时机">
                <div>
                    <div>
                        <el-radio-group v-model="formData.deduct_stock">
                            <el-radio :label="1">订单交易占用库存</el-radio>
                        </el-radio-group>
                    </div>
                    <div class="form-tips">订单提交占用库存，库存不足则无法提交订单</div>
                </div>
            </el-form-item>
            <el-form-item label="取消订单退回库存">
                <div>
                    <div>
                        <el-radio-group v-model="formData.cancel_order_return_stock">
                            <el-radio :label="1">退回库存</el-radio>
                            <el-radio :label="2">无需退回库存</el-radio>
                        </el-radio-group>
                    </div>
                    <div class="form-tips">订单提交占用库存，库存不足则无法提交订单</div>
                </div>
            </el-form-item>
        </el-form>
    </el-card>
    <footer-btns>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
    </footer-btns>
</template>
<script setup lang="ts">
import { TransactionSettings, getTransaction } from '@/api/setting/deal'
const formData = reactive({
    cancel_unpaid_orders: 1,
    cancel_unpaid_orders_times: 30,
    cancel_paid_orders: 1,
    cancel_paid_orders_times: 30,
    after_sales: 1,
    after_sales_times: 7,
    deduct_stock: 1,
    cancel_order_return_stock: 1,
    //TODO
    finish_order: 1,
    finish_order_times: 30
})
const handleSubmit = async () => {
    try {
        await TransactionSettings(formData)
        getData()
    } catch (error) {
        console.log('保存=>', error)
    }
}
const getData = async () => {
    try {
        const data = await getTransaction()
        for (const key in formData) {
            //@ts-ignore
            formData[key] = data[key]
        }
    } catch (error) {
        console.log('获取=>', error)
    }
}
getData()
</script>
<style scoped lang="scss">
.el-alert--info {
    color: rgba(64, 115, 250, 1);
    background-color: rgba(246, 248, 255, 1);
}
</style>
